﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tigera.LibCommon;
using Tigera.T_Definition;
using Tigera.LibCodeUtils;
using Tigera.Network;
using System.Windows.Forms;

namespace Plugin.TradeSharp
{

    public class ClientMQConnection : MQConnection
    {
        public ClientMQConnection()
            : this(Guid.NewGuid().ToString(), NetStateHandler.DefaultInstance)
        {
            this.MessageReceived += OnMessageReceived;
            //this.NetStateChanged += new EventHandler<TiEventArgs<NetState>>(HSMQConnection_NetStateChanged);
            this.Statehandler.Errored += new EventHandler<TiEventArgs<Tuple<NetError, string, Exception, MQSession>>>(Statehandler_Errored);
        }

        void Statehandler_Errored(object sender, TiEventArgs<Tuple<NetError, string, Exception, MQSession>> e)
        {
            string errmsg = e.Data.Item2 + "\n" + e.Data.Item3.Message.ToString();
            MessageBox.Show(null, errmsg, "网络错误", MessageBoxButtons.OK);
            this.PhenixBorn(e);
        }
        
        void HSMQConnection_NetStateChanged(object sender, TiEventArgs<NetState> e)
        {
            //string errmsg = e.Data.Item2 + "\n" + e.Data.Item3.Message.ToString();
            //MessageBox.Show(null, errmsg, "网络错误", MessageBoxButtons.OK);
            //this.PhenixBorn(e);
        }

        public void SendCMD(TradeCMD cmd)
        {
            AppMgr.Instance.Mqclient.SendDataTo(AppMgr.TradeRiskExName, ESHelper.Pack(ref cmd));
            
        }
        public void SendAny(T_AnyObjResponse obj)
        {
            AppMgr.Instance.Mqclient.SendDataTo(AppMgr.TradeRiskExName, ESHelper.Pack(ref obj));
        }
        
        public event EventHandler<TiEventArgs<bool>> OnLoginResponse;
        public void OnMessageReceived(object sender, MessageEventArgs e)
        {
            if (e.Data.TypeID == (int)MessageID.LogonResponse)
            {

                var res = e.Data.Cast<LogonResponse>();
                if (res.Result == LogonResult.Succeed)
                {
                    AppMgr.Instance.Logined = true;
                }
                if (OnLoginResponse != null)
                {
                    OnLoginResponse(this, new TiEventArgs<bool>(res.Result == LogonResult.Succeed));
                }


            }
            else if (e.Data.TypeID == (int)MessageID.T_AnyObjResponse)
            {
                var res = e.Data.Cast<T_AnyObjResponse>();
                Process(res);
            }
            else if (e.Data.TypeID == (int)MessageID.ServerStatusResponse)
            {
                //var res = e.Data.Cast<ServerStatusResponse>();
                //if(res.T_ServerStatus.)

                //   if (e.From == AppMgr.MainExchange)
                // {

                //}
                //if(e.Data.)
            }
            else if (e.Data.TypeID == (int)MessageID.T_MQConnectionInfoResponse)
            {
                var info = e.Data.Cast<T_MQConnectionInfoResponse>();

                if (info.TradeRimsServerStatus != traderimsstatus)
                {
                    traderimsstatus = info.TradeRimsServerStatus;
                    
                   // MessageBox.Show(null, "目前服务器状态" + traderimsstatus.ToString(),"提示", MessageBoxButtons.OK);
                }
                if (OnConInfo != null)
                {
                    OnConInfo(this, new TiEventArgs<T_MQConnectionInfoResponse>(info));
                }
            }
            else if (e.Data.TypeID == (int)MessageID.T_NotificationResponse)
            {
                var info = e.Data.Cast<T_NotificationResponse>();
                if (OnNotificationResponse != null)
                {
                    OnNotificationResponse(this, new TiEventArgs<T_NotificationResponse>(info));
                }
            }
            
        }

        public event EventHandler<TiEventArgs<T_NotificationResponse>> OnNotificationResponse;
        public event EventHandler<TiEventArgs<T_MQConnectionInfoResponse>> OnConInfo;

       public ComputerStatus traderimsstatus = ComputerStatus.OnLine;

        public ClientMQConnection(string myID, NetStateHandler statehandler)
            : base(myID, statehandler)
        {

        }
        public event EventHandler<TiEventArgs<List<TDealedOrder>>> OnList_ResultDealedOrder;
        public event EventHandler<TiEventArgs<NetBasicAccountInfo>> OnBasicAccountInfo;
        public event EventHandler<TiEventArgs<List<TSPosition>>> OnList_Position;
        public event EventHandler<TiEventArgs<ResponseTadeRiskGloStateQuery>> OnResponseTadeRiskGloStateQuery;

        public event EventHandler<TiEventArgs<RiskSettingInfo>> OnResponseRiskSettingInfoQuery;
        void Process(T_AnyObjResponse res)
        {
            if (res.DataType == (int)TradeRiskOpe.ResponseTodayBusinessRecordQuery)
            {
                var ret = res.SerialCast<ResponseTodayBusinessRecordQuery>();
                if (OnList_ResultDealedOrder != null)
                {
                    OnList_ResultDealedOrder(this, new TiEventArgs<List<TDealedOrder>>(ret.Items));
                }
            }
            else if (res.DataType == (int)TradeRiskOpe.ResponseTradeQueryAccountInfoQuery)
            {
                var ret = res.SerialCast<NetBasicAccountInfo>();
                if (OnBasicAccountInfo != null)
                {
                    OnBasicAccountInfo(this, new TiEventArgs<NetBasicAccountInfo>(ret));
                }
            }
            else if (res.DataType == (int)TradeRiskOpe.ResponseAccountPositionQuery)
            {
                var ret = res.SerialCast<ResponseAccountPositionQuery>();
                if (OnList_Position != null)
                {
                    OnList_Position(this, new TiEventArgs<List<TSPosition>>(ret.Items));
                }
            }
            else if (res.DataType ==(int)TradeRiskOpe.ResponseTadeRiskGloStateQuery)
            {
                var ret = res.SerialCast<ResponseTadeRiskGloStateQuery>();
                if (OnResponseTadeRiskGloStateQuery != null)
                {
                    OnResponseTadeRiskGloStateQuery(this, new TiEventArgs<ResponseTadeRiskGloStateQuery>(ret));
                }
            }
            else if (res.DataType == (int)TradeRiskOpe.ResponseRiskSettingQuery)
            {
                var ret = res.SerialCast<RiskSettingInfo>();
                if (OnResponseRiskSettingInfoQuery != null)
                {
                    OnResponseRiskSettingInfoQuery(this, new TiEventArgs<RiskSettingInfo>(ret));
                }
            }

        }

    }

}
